home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Eagles Nest BBS 8
/
Eagles_Nest_Mac_Collection_Disc_8.TOAST
/
Developer Tools⁄Additions
/
IntermediatC
/
Queue #9
/
queue 2.c
next >
Wrap
C/C++ Source or Header
|
1990-11-07
|
6KB
|
220 lines
/*
WRITE A PROGRAM WHICH DOES WHAT IS DESCRIBED BELOW.
YOU ARE GIVEN THE TOP HALF OF THIS PROGRAM [UP TO main()].
The program must call simulation() TIME_LIMIT times. In this simulation,
the arrival and service time is decremented. If arrival reaches 0, add a
ship and generate a new arrival time. Be careful, the new time could be 0
too. If this occurs, the new ship must also be added. Do the same for
service, which removes the ship at the head of the queue. A ship is added
to the end of the queue and removed from the beginning of the queue.
An variable will track the number of ships on the queue. No more ships can be
added is the variable count reaches QUEUE_SIZE. A variable also points to
the head and tail of the queue. A ship is to be removed by incrementing the head
variable (so it points to the next ship in line). If either head or tail reaches
the end of the queue, it wraps to the beginning (so that it is always possible
to have QUEUE_SIZE ships in the queue).
Each ship will have a different hull number which is created by incrementing
new_number. The name is a random combination of 3 character arrays. Generate 3
random numbers and combine the 3 resulting names (using the numbers as subscripts
for the strings) into a single name and give that name to the ship. For example,
if the 3 random numbers are 2, 3, and 0, the resulting ship name is
Exxon America I.
Display the ship list each time a ship is added.
*/
/*
*
* This is a program to study a circular queue of ships
* waiting to pass through a lock. Each ship is created
* at some random arrival interval and placed on the queue.
* At other random intervals, the queue is serviced and the
* first ship in line is removed. In theory, removal would
* transfer the ship to the lock, but in this problem the ship
* is just destroyed.
* The ships on the queue are displayed each time a new ship arrives.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define QUEUE_SIZE 10 /* maximum number of waiting ships */
#define NAME_SIZE 40 /* length of each ship's name */
#define MAX_ARRIVAL 12 /* maximum arrival interval */
#define MAX_SERVICE 24 /* maximum service interval */
#define TIME_LIMIT 100 /* run of simulation (0 to TIME_LIMIT) */
#define random(x) (rand() % (x) )
typedef struct ship_type
{
int hull_number;
char name[NAME_SIZE];
} ship_type;
typedef struct queue_type
{
int count;
int head;
int tail;
ship_type ship[QUEUE_SIZE];
} queue_type;
static void simulation (void); /* called by main TIME_LIMIT times */
static ship_type create_ship (void); /* returns a new ship */
static void add_ship (ship_type new_ship); /* adds ship to queue */
static void remove_ship (void); /* removes by changing head */
static void display_queue (void); /* lists ships on queue, followed by newline */
static queue_type queue;
static int clock; /* clock goes from 0 to TIME_LIMIT */
static int arrival; /* next arrival interval */
static int service; /* next service interval */
static FILE * file_stream; /* file control block pointer */
static int new_number = 101; /* hull numbers for newly created ships */
static char * first[] =
{
"U. S. S. ",
"HMS ",
"Exxon ",
"Yacht ",
"SS ",
"Royal ",
"Tug "
};
static char * second[] =
{
"Dreadnaught ",
"Valdez ",
"Annie ",
"America ",
"Scotland ",
"Ille de France ",
"Voyager "
};
static char * third[] =
{
"I",
"II",
"III",
"IV",
"V",
"VI",
"VII"
};
/* ------------------------------------------------------------------------------- */
main()
{
file_stream = fopen("ship queue.out", "w");
arrival = random(MAX_ARRIVAL);
service = random(MAX_SERVICE);
for (clock = 0; clock < TIME_LIMIT; clock++)
{
simulation();
arrival--;
service--;
}
fclose(file_stream);
}
/* ------------------------------------------------------------------------------- */
static void simulation (void)
{
ship_type new_ship;
while ((!arrival) || (!service))
{
if (!arrival)
{
if (queue.count < QUEUE_SIZE)
{
new_ship = create_ship();
add_ship( new_ship );
display_queue();
}
arrival = random(MAX_ARRIVAL);
}
if (!service)
{
if (queue.count > 0)
remove_ship();
service = random(MAX_SERVICE);
}
}
}
/* ------------------------------------------------------------------------------- */
static ship_type create_ship (void)
{
ship_type new_ship;
new_ship.hull_number = new_number++;
strcpy( (char *) new_ship.name, (char *) first[random(7)] );
strcat( (char *) new_ship.name, (char *) second[random(7)] );
strcat( (char *) new_ship.name, (char *) third[random(7)] );
return new_ship;
}
/* ------------------------------------------------------------------------------- */
static void add_ship (ship_type new_ship)
{
queue.count++;
queue.ship[queue.tail] = new_ship;
if ((++queue.tail) >= QUEUE_SIZE)
queue.tail = 0;
}
/* ------------------------------------------------------------------------------- */
static void remove_ship (void)
{
queue.count--;
if ((++queue.head) >= QUEUE_SIZE)
queue.head = 0;
}
/* ------------------------------------------------------------------------------- */
static void display_queue (void)
{
int i;
int temp;
for (i = 0, temp = queue.head; i < queue.count; i++)
{
fprintf(file_stream, "%4d %s\n", queue.ship[temp].hull_number,
(char *) queue.ship[temp].name);
temp++;
if (temp >= QUEUE_SIZE)
temp = 0;
}
fprintf(file_stream, "\n");
}